<?php

function uploadimage($files, $dir)
{
    $error = "";
    $image =array();
    // $files moet een een array zijn
    if(is_array($files))
    {
        // files in alfabetische volgorde zetten
        sort($files);

        foreach($files as $file)
        {
            if(is_uploaded_file($file['tmp_name']))
            {

                chdir('../../content/data/images/profiles/');

                // controleer of directory "$article" bestaat
                if(!file_exists($dir))
                {
                    // id gebruiken als directorynaam
                    if(mkdir(strtolower($dir), 0777))
                    {
                        // echo "Directory ".$dir." is aangemaakt<br />";

                        if(!chmod(strtolower($dir), 0777))
                        {
                            $uploadError = "Unable to store file in desired directory ".$dir."<br />
                             (directory is probably write protected.)<br />";
                        }
                    }
                }

                chdir('./'.strtolower($dir).'/');
                if(!file_exists('avatars')){
                    mkdir('avatars/');
                    chmod('avatars/', 0777);
                }
                chdir('./avatars');
                if(!file_exists('small')){
                    mkdir('small/');
                    chmod('small/', 0777);
                }

                // controleer of het bestand "$file['name']" bestaat
                if(file_exists($file['name']))
                {
                    $uploadError = "The file ".$file['name']." already exists in your avatar library.<br />";
                    return false;
                }

                //                // The file
                //                $filename = strtolower($file['name']);
                //
                //                // Set a maximum height and width
                //                $width = 100;
                //                $height = 100;
                //
                //                // Content type
                //                header('Content-type: image/jpeg');
                //
                //                // Get new dimensions
                //                list($width_orig, $height_orig) = getimagesize($filename);
                //
                //                $ratio_orig = $width_orig/$height_orig;
                //
                //                if ($width/$height > $ratio_orig) {
                //                    $width = $height*$ratio_orig;
                //                } else {
                //                    $height = $width/$ratio_orig;
                //                }
                //
                //                // Resample
                //                $image_p = imagecreatetruecolor($width, $height);
                //                $image = imagecreatefromjpeg($filename);
                //                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                //
                //                // Output
                //                imagejpeg($image_p, null, 100);


                if(move_uploaded_file($file['tmp_name'], strtolower($file['name'])))
                {
                    if(!chmod($file['name'], 0777))
                    {
                        $uploadError = "File is write protected.<br />";
                    }

                    $image = $file['name'];
                    //return $image;
                }
                
                if($uploadError == ""){
                    $uploadError = "Congratulations, upload was succesfull";
                }
                return $uploadError;
            }
        }
    }
    else
    {
        return false;
    }
}

?>